package cn.itcast.eduservice.service.impl;

import cn.itcast.eduservice.entity.EduSubject;
import cn.itcast.eduservice.entity.excel.SubjectData;
import cn.itcast.eduservice.entity.subject.OneSubject;
import cn.itcast.eduservice.entity.subject.TwoSubject;
import cn.itcast.eduservice.listener.SubjectExcelListener;
import cn.itcast.eduservice.mapper.EduSubjectMapper;
import cn.itcast.eduservice.service.EduSubjectService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author Duanzihuang
 * @since 2020-05-04
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            InputStream in = file.getInputStream();

            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        // 查询所有一级分类 parent_id = 0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        // 查询所有二级分类 parent_id != 0
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        // 创建list集合，用于存储最终封装数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        // 封装一级分类
        // 查询出所有的一级分类list集合遍历，得到每个一级分类对象，获取每个一级分类的值
        // 封装到要求的list集合里面 List<OneSubject> finalSubjectList
        for (int i = 0; i <oneSubjectList.size() ; i++) {
            EduSubject eduSubject = oneSubjectList.get(i);

            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(eduSubject,oneSubject);

            finalSubjectList.add(oneSubject);

            // 遍历二级分类
            List<TwoSubject> finalTwoSubjectList = new ArrayList<>();
            for (int m = 0; m <twoSubjectList.size() ; m++) {
                EduSubject eduSubject2 = twoSubjectList.get(m);

                if (eduSubject2.getParentId().equals(eduSubject.getId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(eduSubject2,twoSubject);

                    finalTwoSubjectList.add(twoSubject);
                }
            }

            // 设置一级分类的children
            oneSubject.setChildren(finalTwoSubjectList);
        }

        return finalSubjectList;
    }
}
